package com.abing.starry.spi.monitor;

import com.abing.starry.spi.monitor.model.SystemInfo;
import com.abing.starry.spi.socket.manager.MessagingRequest;
import com.abing.starry.spi.socket.subscribe.SubscriptionProvider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.util.Map;

/**
 * @Author CaptainBing
 * @Date 2024/9/25 16:03
 * @Description
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class SystemMonitorProvider implements SubscriptionProvider {

    private final SystemMonitorService systemMonitorService;

    @Override
    public String id() {
        return "dashboard-system-monitor";
    }

    @Override
    public String name() {
        return "仪表盘控制系统信息";
    }

    @Override
    public String[] getTopicPattern() {
        return new String[]{"/dashboard/system/monitor/realtime"};
    }

    @Override
    public Flux<SystemInfo> subscribe(MessagingRequest messagingRequest) {
        log.info("SystemMonitorProvider subscribe");
        Map<String, Object> parameter = messagingRequest.getParameter();
        String interval = String.valueOf(parameter.getOrDefault("interval", 2));

        return Flux.interval(Duration.ofSeconds(Long.parseLong(interval)))
                   .flatMap(i -> systemMonitorService.system());
    }
}
